
import Vue from 'vue'
import app from "./app.vue"
import router from './router'
import axios from 'axios'
import VueRouter from 'vue-router'
import Vuex from 'vuex'

Vue.use(VueRouter)
Vue.use(Vuex)

Vue.prototype.$ajax = axios; // 在组件中使用  this.$ajax.get("").then(res=>{}).catch(err=>{})
axios.defaults.baseURL = 'http://49.235.95.107/api'; // 腾讯云服务器地址，开放8000端口

let store = new Vuex.Store({
    state: {
        userinfos: null,
        fav: null,
        order: [],
        shopcar: []
    },
    mutations: {
        userinfosStore(state, userinfos) {
            this.state.userinfos = userinfos
        },
        favStore(state, favlist) {
            this.state.fav = favlist
        },
        orderStore(state, orderlist) {
            this.state.order = orderlist
        },
        shopcarStore(state, shopcarlist) {
            this.state.shopcar = shopcarlist
        }
    },
    getters: {

    }
})
import { Button } from 'mint-ui';
import { Header } from 'mint-ui';
import 'mint-ui/lib/style.css'
import { Swipe, SwipeItem } from 'mint-ui';
import { Lazyload } from 'mint-ui';
import { Popup } from 'mint-ui';
import { Field } from 'mint-ui';
import { Toast } from 'mint-ui';
import { Indicator } from 'mint-ui';
import { InfiniteScroll } from 'mint-ui';
import { Switch } from 'mint-ui';

Vue.component(Swipe.name, Swipe);
Vue.component(SwipeItem.name, SwipeItem);
Vue.component(Button.name, Button);
Vue.component(Header.name, Header);
Vue.use(Lazyload);
Vue.use(Toast);
Vue.use(Indicator);
Vue.use(InfiniteScroll);
Vue.component(Popup.name, Popup);
Vue.component(Field.name, Field);
Vue.component(Switch.name, Switch);


//http REQUEST 拦截器   -- 每次request都带token 在访问不需要认证的接口时401--目前在每个组件中单独使用token
axios.interceptors.request.use(config => {
    // Do something before request is sent
    let token = window.localStorage.getItem("token")
    if (token !== null) {
        config.headers.Authorization = `JWT ${token}`;
        return config;
    } else {
        return config;
    }


}, error => {
    // Do something with request error
    return Promise.reject(error);
});
//http response 拦截器
axios.interceptors.response.use(
    undefined,
    error => {
        let res = error.response;
        switch (res.status) {
            case 400:
                return Promise.reject({
                    "detail": 400
                })
            case 401:
                return Promise.reject({
                    "detail": 401
                })
            //console.log('未登录 或者token过期');
            case 403:
                //console.log('您没有该操作权限');
                return Promise.reject({
                    "detail": "您没有该操作权限"
                })
            // alert('您没有该操作权限');
            case 500:
                //console.log('服务器错误');
                return Promise.reject({
                    "detail": "服务器错误"
                })
            // alert('服务器错误');
        }
        return Promise.reject(error.response.data) // 返回接口返回的错误信息
    });


var vm = new Vue({
    el: "#app",
    render: c => c(app),
    router, //挂载抽离的路由文件--router.js
    store //挂载vuex实例--store状态管理对象
});